package com.bjpowernode.crm.workbench.service.serviceimpl;

import com.bjpowernode.crm.setting.dao.UserDao;
import com.bjpowernode.crm.setting.eneity.User;
import com.bjpowernode.crm.util.DateTimeUtil;
import com.bjpowernode.crm.util.UUIDUtil;
import com.bjpowernode.crm.vo.PageinationVO;
import com.bjpowernode.crm.workbench.dao.*;
import com.bjpowernode.crm.workbench.eneity.*;
import com.bjpowernode.crm.workbench.exception.TranException;
import com.bjpowernode.crm.workbench.service.TranService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class TranServiceImpl implements TranService {
    @Autowired
    private TranDao tranDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private CustomerDao customerDao;
    @Autowired
    private TranHistoryDao tranHistoryDao;
    @Autowired
    private ActivityDao activityDao;
    @Autowired
    private ContactsDao contactsDao;
    @Override
    public PageinationVO<Tran> pageList(Map<String, String> params) {
        //获取参数，页码以及每页记录条数
        String pageNo=params.get("pageNo");
        String pageSize=params.get("pageSize");
        //获取总记录条数
        int title=tranDao.getTitle();
        //再获取否和条件的交易信息列表
        PageHelper.startPage(Integer.valueOf(pageNo),Integer.valueOf(pageSize));
        List<Tran> tranList=tranDao.getTranList(params);
        //将结果封装到vo中
        PageinationVO vo=new PageinationVO();
        vo.setTitle(title);
        vo.setDataList(tranList);
        return vo;
    }

    @Override
    public List<User> getUserList() {
        //调用dao
        List<User> userList=userDao.selectUsers();
        return userList;
    }

    @Override
    @Transactional
    public boolean save(Map<String, String> params) throws TranException {
        boolean flag=true;
        //完善交易参数,获取客户id
        String customerName=params.get("customerName");
        //根据客户列表有无该客户，有就使用，无就新建
        Customer customer =customerDao.getCustomerByName(customerName);
        if (customer==null){
            customer=new Customer();
            customer.setId(UUIDUtil.getUUID());
            customer.setOwner(params.get("owner"));
            customer.setName(customerName);
            customer.setCreateBy(params.get("createBy"));
            customer.setCreateTime(DateTimeUtil.getSysTime());
            customer.setContactSummary(params.get("contactSummary"));
            customer.setDescription(params.get("description"));
            customer.setNextContactTime(params.get("nextContactTime"));
            //调用dao，新建客户
            int result=customerDao.insertCustomer(customer);
            if (result!=1){
                flag=false;
                throw new TranException("交易添加失败");
            }
        }
        //根据客户id，完善交易参数
        params.put("customerId",customer.getId());
        //调用dao，添加交易
        int result1=tranDao.saveTran(params);
        if (result1!=1){
            flag=false;
            throw new TranException("交易添加失败");
        }
        //交易添加完成，添加一条交易历史
        TranHistory history=new TranHistory();
        history.setId(UUIDUtil.getUUID());
        history.setTranId(params.get("id"));
        history.setStage(params.get("stage"));
        history.setMoney(params.get("money"));
        history.setExpectedDate(params.get("expectedDate"));
        history.setCreateTime(DateTimeUtil.getSysTime());
        history.setCreateBy(params.get("createBy"));
        int result3=tranHistoryDao.insertHistory(history);
        if (result3!=1){
            flag=false;
            throw new TranException("交易添加失败");
        }
        return flag;
    }

    @Override
    @Transactional
    public Map<String,Object> getTranById(String id) {
        Map<String,Object> detailMap=new HashMap<>();
        //调用tran的dao，查出交易详细信息
        Tran tran=tranDao.getTranById(id);
        //调用activity,contacts,customer的dao，分别查询出名称
        Activity activity=activityDao.selectById(tran.getActivityId());
        String activityName="";
        if (activity!=null){
            activityName=activity.getName();
        }
        String customerName=customerDao.selectById(tran.getCustomerId());
        String contactsName=contactsDao.selectById(tran.getContactsId());
        if ("".equals(customerName)||contactsName==null){
            contactsName="";
        }
        detailMap.put("tran",tran);
        detailMap.put("activityName",activityName);
        detailMap.put("customerName",customerName);
        detailMap.put("contactsName",contactsName);
        return detailMap;
    }

    @Override
    public List<TranHistory> getTranHistoryList(String tranId) {
        List<TranHistory> historyList=tranHistoryDao.getTranHistoryList(tranId);
        return historyList;
    }

    @Override
    @Transactional
    public Boolean changeStage(Map<String, String> params) throws TranException {
        Boolean flag=true;
        //调用dao,进行交易记录阶段的修改
        int result=tranDao.changeStage(params);
        if (result!=1){
            flag=false;
            throw new TranException("交易阶段变更失败");
        }
        TranHistory history=new TranHistory();
        history.setId(UUIDUtil.getUUID());
        history.setCreateBy(params.get("createBy"));
        history.setCreateTime(params.get("createTime"));
        history.setExpectedDate(params.get("expectedDate"));
        history.setStage(params.get("stage"));
        history.setTranId(params.get("id"));
        history.setMoney(params.get("money"));
        int result2=tranHistoryDao.insertHistory(history);
        if (result2!=1){
            flag=false;
            throw new TranException("交易阶段变更失败");
        }
        return flag;
    }

    @Override
    public PageinationVO<Map<String,String>> getTranStageAndCount() {
        PageinationVO<Map<String,String>> vo=new PageinationVO<>();
        //调用service，查询总记录交易条数
        int title=tranDao.getTitle();
        //调用service，查询每个阶段的记录总数
        List<Map<String,String>> mapList=tranDao.getTranStageAndCount();
        vo.setTitle(title);
        vo.setDataList(mapList);
        return vo;
    }
}
